// Modified by Balaji for Firealarm project (19.07.2012)


// Digital Library for Pinguino
// jean-pierre MANDON 2008
// modification 2009/08/08 18F4550
// regis blanchot 2011/08/09 : FreeJALduino support
// regis blanchot 2012/02/14 : Pinguino 26J50 support
// regis blanchot 2012/04/23 : new digitalread (thanks to JP Mandon)

#ifndef __DIGITALW__
#define __DIGITALW__

#include <GenericTypeDefs.h>
#include <pic18f26j50.h>

#define pB	0
#define pC	1
#define pA	2
#define pD	3
#define pE	4
#define pF	5
#define pG	6

#define _0	1<<0	// 0x01
#define _1	1<<1	// 0x02
#define _2	1<<2	// 0x04
#define _3	1<<3	// 0x08
#define _4	1<<4	// 0x10
#define _5	1<<5	// 0x20
#define _6	1<<6	// 0x40
#define _7	1<<7	// 0x80

const UINT8 mask[18]={	_0,_1,_2,_3,_4,_5,_6,_7,_6,_7,_0,_1,_2,	_0,_1,_2,_3,_5};

const UINT8 port[18]={	pB, pB, pB, pB, pB, pB, pB, pB,	pC, pC, pC, pC, pC,	pA, pA, pA, pA, pA};
/*
void digitalwrite(int output,int state)
{
	switch (port[output])
	{
		case pB: if (state) PORTB=PORTB | mask[output]; 
				else PORTB=PORTB & (255-mask[output]);
				break;
		case pC: if (state) PORTC=PORTC | mask[output];
				else PORTC=PORTC & (255-mask[output]);
				break;
		case pA: if (state) PORTA=PORTA | mask[output];
				else PORTA=PORTA & (255-mask[output]);
				break;
	}
}


UINT8 digitalread(UINT8 pin)
{
	UINT16 reg;

	switch (port[pin])
	{
		case pB:
			reg = PORTB;
			break;
		case pC:
			reg = PORTC;
			break;
		case pA:
			reg = PORTA;
			break;
		
	}
	if ((reg & mask[pin])!=0)
		return 1;
	else
		return 0;
}

void pinmode(int input, int state)
{
	switch (port[input])
	{
		case pB: if (state) TRISB=TRISB | mask[input];
			else TRISB=TRISB & (255-mask[input]);
			break;
		case pC: if (state) TRISC=TRISC | mask[input];
			else TRISC=TRISC & (255-mask[input]);
			break;
		case pA: if (state) TRISA=TRISA | mask[input];
			else TRISA=TRISA & (255-mask[input]);
			break;		
	}
}

void toggle(UINT8 pin)
{
	UINT8 val;
	val = digitalread(pin);
	digitalwrite(pin, val^1);
}

*/